package com.yanqu.road.server.logic.activity.condition.chuhan;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.pb.activity.ChuHanProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.math.BigInteger;
import java.util.List;

public class ChuHanDailyOccupyCityCondition extends BaseActivityCondition {

    public ChuHanDailyOccupyCityCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
        super(activityInfo, activityConditionInfo, baseActivityData);
    }

    @Override
    public boolean isCompleted(GamePlayer player) {
        if (!ActivityMgr.activityInTime(activityInfo)) {//活动期内才能领奖
            return false;
        }
        //等于1就是完成了
        return getValue().compareTo(BigInteger.ONE) == 0;
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        ChuHanProto.ChuHanOrderCityTaskSyncMsg syncMsg = (ChuHanProto.ChuHanOrderCityTaskSyncMsg) object;
        //参数配置说明:军令序号|0|0|军令类型（1战略要地|2郡城|3县城）0无|是否每日重置
        List<BigInteger> paramList = activityConditionInfo.getParamList();
        int checkOrderType = paramList.get(3).intValue();
        int checkIdx = paramList.get(0).intValue();


        for (ChuHanProto.ChuHanOrderCityTaskTemp taskTemp : syncMsg.getTaskInfosList()) {
            if (taskTemp.getOrderCityType() == checkOrderType) {
                if (checkIdx > 0 && checkIdx <= taskTemp.getCityInfosCount()) {
                    ChuHanProto.ChuHanOrderCityTemp cityInfos = taskTemp.getCityInfos(checkIdx - 1);
                    String cityId = String.valueOf(cityInfos.getCityId());
                    String oldCityId = getParam();
                    setParam(cityId);
                    BigInteger oldValue = getValue();
                    if (ActivityMgr.activityInTime(activityInfo)) {//活动期内才能变更成可领奖
                        if (cityInfos.getIsOccupy()) {
                            setValue(BigInteger.ONE);
                        } else {
                            setValue(BigInteger.ZERO);
                        }
                    }
                    if (oldValue.compareTo(getValue()) != 0 || !oldCityId.equals(cityId)) {
                        setModifyTime(DateHelper.getCurrentTime() / 1000);
                    }
                    break;
                }
            }
        }
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.ChuHanDailyOccupyCity.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.ChuHanDailyOccupyCity.getValue(), handle);
    }

    @Override
    public boolean canChange(ActivityInfo curActivity) {
        return ActivityMgr.activityInShowTime(curActivity);
    }
}
